package org.cypress.rtpack.controller;
import org.cypress.rtpack.VO.SimRep;
import org.cypress.rtpack.common.core.Result;
import org.cypress.rtpack.entity.RtpackHostname;
import org.cypress.rtpack.service.IRtpackHostnameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author getty
 */
@RestController
@RequestMapping("/guard")
public class HeartController{
    @Autowired
    private IRtpackHostnameService rtpackHostnameService;

    @GetMapping("/isRun/0")
    public Result<SimRep> AskIsRun(String id) {
        SimRep simRep = new SimRep();
        simRep.setCode(0);
        simRep.setMsg("success");
        return Result.dataOf(simRep);
    }

    // 允许所有来源访问此接口,已复制到RtpackHostnameController中，保持客户端可用，暂且保留
    @CrossOrigin
    @GetMapping("/tell_me_position")
    public Result<SimRep> getClientPosition(HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        RtpackHostname rtpackHostname = rtpackHostnameService.getByIP(ipAddress);
        if(rtpackHostname == null) {
            rtpackHostname = new RtpackHostname();
            rtpackHostname.setIndex(0);
            rtpackHostname.setIP(ipAddress);
            rtpackHostname.setHostname(ipAddress);
        }
        return Result.dataOf(rtpackHostname);
    }

}
